package org.zecontech.extraplus;

import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.ai.goal.target.NonTameRandomTargetGoal;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.animal.horse.AbstractHorse;
import net.minecraft.world.entity.animal.horse.SkeletonHorse;
import net.minecraft.world.entity.animal.horse.ZombieHorse;
import net.minecraft.world.entity.animal.wolf.Wolf;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.entity.EntityJoinLevelEvent;
import net.neoforged.neoforge.event.entity.living.LivingDeathEvent;
import net.neoforged.neoforge.event.entity.player.AttackEntityEvent;
import org.zecontech.extraplus.api.event.MobInitGoalEvent;
import org.zecontech.extraplus.entity.Kelong;
import org.zecontech.extraplus.init.ModItems;

/**
 * 模组事件处理器
 */
@EventBusSubscriber(modid = ExtraPlus.MOD_ID)
public class ExtraPlusEventHandler {

    /**
     * 玩家攻击实体事件处理
     * @param event 攻击实体事件
     */
    @SubscribeEvent
    public static void onPlayerAttack(AttackEntityEvent event) {
        var player = event.getEntity();
        var entity = event.getTarget();
        // 判断是否铁剑
        if (player.getMainHandItem().is(Items.IRON_SWORD)) {
            if (entity instanceof Kelong) {//攻击实体是否为科龙
                player.getMainHandItem().shrink(1);//删除物品
                player.setItemInHand(InteractionHand.MAIN_HAND, new ItemStack(ModItems.SWORD_RED.get()));// 变为剑（红）
            }
        }
    }

    /**
     * 实体死亡事件处理
     * @param event 实体死亡事件
     */
    @SubscribeEvent
    public static void onLivingDeath(LivingDeathEvent event) {
        var entity = event.getEntity();
        var attackMob = entity.getLastAttacker();
        if (attackMob instanceof Player player) {//判断是否玩家攻击
            if (entity instanceof Kelong) {//判断实体是否是科龙
                if (!player.getMainHandItem().is(ModItems.SWORD_RED)) {//判断是否使用剑（红）
                    if (entity.captureDrops() != null) {// NPE 非空判断
                        entity.captureDrops().clear();//清除所有掉落物
                    }
                }
            }
        }
    }

    /**
     * 实体加入世界事件处理
     * @param event 实体加入世界事件
     */
    @SubscribeEvent
    public static void onEntityJoin(EntityJoinLevelEvent event) {
        var entity = event.getEntity(); // 获取实体
        boolean isSpecialHorse = entity instanceof ZombieHorse || entity instanceof SkeletonHorse;// 判断是否特殊马
        if (isSpecialHorse && entity instanceof AbstractHorse abstractHorse) { // 判断是否是AbstractHorse的子类
            abstractHorse.setTamed(true);// 设置马为已驯服
        }
    }

    /**
     * 生物初始化目标事件处理
     * @param event 怪物初始化目标事件
     */
    @SubscribeEvent
    public static void onMobInitGoal(MobInitGoalEvent event) {
        var mob = event.getMob(); // 获取生物
        var targetSelector = event.getTargetSelector(); // 获取目标选择器
        if (mob instanceof Wolf wolf) { // 检测是否是狼
            targetSelector.addGoal(5,
                    new NonTameRandomTargetGoal(wolf, Animal.class, true, (livingEntity, serverLevel) ->
                            livingEntity.getType() == EntityType.CHICKEN));// 添加目标选择器
        }
    }
}
